$(function() {
	var duizhangstate;
	var page = 1;
	var fi=1;
	var main=[[ //表头
		{field: 'order_no',width:'200', title: '订单编号',  sort: true, fixed: 'left'}
		,{field: 'ride_time',width:'200',  title: '出发时间',  sort: true, fixed: 'left'}
        ,{field: 'departure',width:'200', title: '出发地点',  sort: true}
        ,{field: 'destination',width:'200', title: '到达地点'} 
        ,{field: 'passenger_name', title: '乘车人'}
        ,{field: 'approval',title: '审批人', sort: true}
        ,{field: 'order_type2',width:'150',title: '订单类型', sort: true}
        ,{field: 'use_explain',title: '用车说明', sort: true}
        ,{field: 'booking_type2',width:'150', title: '订车类型', sort: true}
        ,{field: 'driver_id',  title: '司机', sort: true}
        ,{field: 'car_number',width:'100',title: '车牌', sort: true}
        ,{field: 'order_money',width:'100', title: '结算金额', sort: true}
        ,{field: 'order_state2',width:'100',  title: '订单状态', sort: true}
        ,{field: 'state2',  title: '状态', sort: true}
        ,{ title: '操作', toolbar:'#operation',fixed: 'right'}
       ]]
	var addoperation = ``;
	$('.print').on('click', function() {
		window.print()
	})

	var arr;
	var data={
				id: {$Think.get.id},
				page: page,
				pagenum:10
		}
	accdetail(data)

	function accdetail(data) {
		var detailload = layer.load()
		ajax(api.acc.detail,data,'post').done(function(res){
			console.log(res)
				if(res.code == 1) {
					$('.company_name').html(res.data.company_name)
					$('.month').html(res.data.month)
					duizhangstate = res.data.check_state
					if(duizhangstate == 1) {
						$(".btn-check").html("审核");
						//				$(".btn-check").attr('disabled','1');
						$(".btn-check").removeClass("layui-btn-disabled");
						$('.btn-check').addClass('am-btn-primary tpl-btn-bg-color-success');
					} else if(duizhangstate == 2) {
						$(".btn-check").html("重新审核");
						$(".btn-check").removeClass("layui-btn-disabled ");
						$('.btn-check').addClass('am-btn-primary tpl-btn-bg-color-success');
						//				$('.btn-check').attr("disabled","disabled");
					} else if(duizhangstate == 3) {
						$(".btn-check").html("待确认");
						$(".btn-check").removeClass("am-btn-primary tpl-btn-bg-color-success");
						$('.btn-check').addClass('layui-btn-disabled');
						$('.btn-check').attr("disabled", "disabled");
					} else if(duizhangstate == 4) {
						$(".btn-check").html("已确定");
						$(".btn-check").removeClass("layui-btn-disabled");
						$('.btn-check').addClass('am-btn-primary tpl-btn-bg-color-success');
						$('.btn-check').attr("disabled", "disabled");
					}
					var orderlist={}
					 orderlist.data = res.data.items
					orderlist=orderstate(orderlist).data
					for(var idx = 0; idx < orderlist.length; idx++) {
						if(orderlist[idx].state == 1) {
							orderlist[idx].state2 = '有效'
						} else {
							orderlist[idx].state2 = '无效'
						}
						
					}
					console.log(orderlist)
					table.render({
					  	elem: '#acc_detail'
					    ,cellMinWidth: 80
					    ,cols: main
						,data: orderlist
						,even: true
					});
					if(fi==1){
					 laypage.render({
					    elem: 'page'
					    ,count: res.data.count
						,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
						,theme: '#1E9FFF'
					    ,jump: function(obj, first){
					    	if(!first){
					       		page=obj.curr
		    					data.page=page
		    					pagenum=obj.limit
		    					data.pagenum=pagenum
		    					accdetail(data)
							}else{
								
							}
					    }
					  });
					  fi=2
					 }
					layer.close(detailload)
					//失效
					table.on('tool(acc_detail)', function(obj){
						 var data2 = obj.data; //获得当前行数据
						  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
						  var tr = obj.tr; //获得当前行 tr 的DOM对象
						 console.log(data2)
						  if(layEvent === 'invalid'){ 
						  	layer.open({
							content: '该订单是否失效',
							btn: ['确定', '取消'],
							yes: function() {
								var invalidload = layer.load()
								ajax(api.acc.detail_cancel,{id:data2.id},'post').done(function(res2){
									console.log(res2)
									if(res2.code == 1) {
										layer.close()
										Msg('失效成功')
										accdetail()
									} else {
										Err('失效失败')
									}
								}).fail(function(){
									layer.close(invalidload)
									Err('请求失败')
								})
								
							}
						});
						  }
					})
					
				}else{
					Err(res.msg)
					layer.close(detailload)
				}
		}).fail(function(){
			Err('请求失败')
			layer.close(detailload)
		})
		
		
		
		
		
	}
	$('.btn-check').on('click', function() {
		var checkload = layer.load()
		$.ajax({
			type: "post",
			url: "{:url('adminapi/Check/audit')}",
			data: {
				id: {$Think.get.id}
			},
			success: function(res) {
				layer.close(checkload)
				console.log(res)
				if(res.code == 1) {
					Msg('对账单审核已发送')
					accdetail()
				} else {
					Msg(res.msg)
				}
			}
		});
	})
	$('.btn-back').on('click', function() {
		window.history.back(-1);
	})
})